package com.itsm.xkitsm.liferoot.ems;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.itsm.xkitsm.liferoot.MainAct;
import com.itsm.xkitsm.liferoot.R;
import com.itsm.xkitsm.liferoot.life.WebShowAct;

public class ExpressSearchUI extends Activity implements OnClickListener {
	private Button search_but;
	private Button return_but;
	private EditText express_number;
	private TextView express_company_info;
	private TextView express_id_info;
	private TextView express_message_info;
	private View express_info;
	private Spinner spinner;
	private String company_name;
	private String company_code;
	private ListView lv_express;
	public static int[] int_arr;
	public static String[] str_mes;
	private BaseAdapter_express adapter_express;
	private ImageView img_ems;
	private TextView tv_ems;
	private ImageView img_express;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.express_search);
		int_arr = new int[] { R.drawable.icon_kuaidi_yt,
				R.drawable.icon_kuaidi_st, R.drawable.icon_kuaidi_sf,
				R.drawable.icon_kuaidi_yd, R.drawable.icon_kuaidi_zt,
				R.drawable.icon_kuaidi_ems, R.drawable.icon_kuaidi_zjs };
		str_mes = getResources().getStringArray(R.array.ems_company);
		company_code = ExpressSearchUI.this.getResources().getStringArray(
				R.array.ems_code)[5];
		company_name = ExpressSearchUI.this.getResources().getStringArray(
				R.array.ems_company)[5];

		search_but = (Button) this.findViewById(R.id.search_express_but);
		return_but = (Button) this.findViewById(R.id.return_but);
		express_number = (EditText) this.findViewById(R.id.express_number);
		express_info = this.findViewById(R.id.express_info);
		spinner = (Spinner) this.findViewById(R.id.ems_com_lsit);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.ems_company,
						android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(selectedListener);
		search_but.setOnClickListener(this);
		return_but.setOnClickListener(this);

		img_ems = (ImageView) findViewById(R.id.img_ems);
		img_express = (ImageView) findViewById(R.id.img_express);
		img_express.setOnClickListener(this);
		tv_ems = (TextView) findViewById(R.id.tv_ems);
		lv_express = (ListView) findViewById(R.id.lv_express);
		lv_express.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				img_ems.setImageResource(int_arr[arg2]);
				tv_ems.setText(str_mes[arg2]);

				company_code = ExpressSearchUI.this.getResources()
						.getStringArray(R.array.ems_code)[arg2];
				company_name = ExpressSearchUI.this.getResources()
						.getStringArray(R.array.ems_company)[arg2];
			}
		});
		adapter_express = new BaseAdapter_express(this);
		lv_express.setAdapter(adapter_express);

	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			BaseContorl.doDimssProgressDialog();
			if (StateCode.express_search_status == Constant.Express.NET_LINK_ERROR) {
				BaseContorl.doShowToask(ExpressSearchUI.this, 0,
						R.string.net_error);
				return;
			}
			if (StateCode.express_search_status == Constant.Express.ID_NO_EXIST) {
				BaseContorl.doShowToask(ExpressSearchUI.this, 0,
						R.string.express_number_error);
				return;
			}
			if (StateCode.express_search_status == Constant.Express.SUCCESS_FULL) {
				ExpressInfo express = (ExpressInfo) msg.getData()
						.getSerializable("express");
				express_company_info = (TextView) ExpressSearchUI.this
						.findViewById(R.id.express_company_info);
				express_id_info = (TextView) ExpressSearchUI.this
						.findViewById(R.id.express_id_info);
				express_message_info = (TextView) ExpressSearchUI.this
						.findViewById(R.id.express_message_info);
				express_company_info.setText(company_name);
				express_id_info.setText(express_number.getText().toString());
				express_message_info.setText(express.getOpt_message());
				express_info.setVisibility(View.VISIBLE);
			}
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_express:
			Intent it = new Intent(ExpressSearchUI.this,
					MainAct.class);
			startActivity(it);
			break;
		case R.id.return_but:
			Intent indexIntent = new Intent();
			// indexIntent.setClass(this, IndexUI.class);
			this.startActivity(indexIntent);
			this.finish();
			break;

		case R.id.search_express_but:
			if (!express_number.getText().toString().trim().equals("")) {
				String num=express_number.getText().toString().trim();
				StateCode.express_search_status = 0;
				express_info.setVisibility(View.GONE);
				if (company_name == null) {
					BaseContorl.doShowToask(ExpressSearchUI.this, 0,
							R.string.input_express_company);
					return;
				}
				BaseContorl.doShowProgressDialog(this, R.string.loading);
//				new SearchListener().start();
				Intent intent=new Intent(ExpressSearchUI.this, WebShowAct.class);
				String url = Constant.Express.EXPRESS_API_URL.replace("post_id",
						num.toUpperCase()).replace("name", company_name);
				intent.putExtra("url",url);
				startActivity(intent);
			} else {
				BaseContorl.doShowToask(ExpressSearchUI.this, 0,
						R.string.input_express_number);
			}
			break;
		}

	}

	private AdapterView.OnItemSelectedListener selectedListener = new AdapterView.OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> parent, View v, int position,
				long id) {
			// 用户选择了，记录下用户�?择的公司�?��应的代码
			company_code = ExpressSearchUI.this.getResources().getStringArray(
					R.array.ems_code)[position];
			company_name = ExpressSearchUI.this.getResources().getStringArray(
					R.array.ems_company)[position];
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}
	};

	private class SearchListener extends Thread {

		public void run() {
			ExpressInfo express = new ExpressInfoService().getResponse(
					express_number.getText().toString().trim(), company_code);
			Bundle bundle = new Bundle();
			bundle.putSerializable("express", express);
			Message message = new Message();
			message.setData(bundle);
			handler.sendMessage(message);
		}
	}
}
